function axios (url, type = 'GET', data) {
  type = type.toUpperCase();
  return new Promise(function (resolve, reject) {
    let xhr = null;
    if (window.XMLHttpRequest) {
      xhr = new XMLHttpRequest();
    } else {
      xhr = new ActiveXObject('Microsoft.XMLHTTP'); // eslint-disable-line
    }
    xhr.open(type, url, true);
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4 && xhr.status === 200) {
        resolve(JSON.parse(xhr.responseText));
      } else {
        // reject(xhr);
      }
    };
    // 请求出错
    xhr.addEventListener('error', e => {
      reject(e);
    });
    if (type === 'GET') {
      xhr.send();
    } else {
      xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
      xhr.send(formatParams(data));
    }
  });
  function formatParams (data) {
    const arr = [];
    for (const name in data) {
      arr.push(name + '=' + data[name]);
    }
    return arr.join('&');
  }
}

export default axios;
